﻿using UnityEngine;
using System.Collections;

public class goalScript : MonoBehaviour {
	
	Color activeCol;
	Color inactiveCol;
	
	Transform colidedPlayer;
	public bool activated;
	public Renderer myRend;
	
	GameManagerScript GMS;
	
	// Use this for initialization
	void Start () {
		activeCol = new Color(0.94f,0.36f,0.47f,1.0f);
		inactiveCol = myRend.material.color;
		if(GameObject.FindGameObjectWithTag("SceneManager") != null)
		{
			GMS = GameObject.FindGameObjectWithTag("SceneManager").GetComponent<GameManagerScript>();
			if(GMS.myGoals[0] == null)
				GMS.myGoals[0] = this;
			else
				GMS.myGoals[1] = this;
		}
	}
	
	// Update is called once per frame
	void Update () {
		if(colidedPlayer)
		{
			if(colidedPlayer.position.y < transform.position.y)
			{
				activate(false);
				networkView.RPC("activate",RPCMode.Others,false);
			}
		}
	}
	
	void OnTriggerStay(Collider myCol)
	{
		if(activated)
			return;
		
		if(myCol.tag == "Player")
		{
			if(myCol.transform.position.y > transform.position.y)
			{
				activate(true);
				networkView.RPC("activate",RPCMode.Others,true);
				colidedPlayer = myCol.transform;
			}
		}
		
	}
	
	void OnTriggerExit(Collider myCol)
	{
		if(activated)
		{
			activate(false);
			networkView.RPC("activate",RPCMode.Others,false);
		}
	}
	
	[RPC]
	void activate(bool isActive)
	{
		activated = isActive;
		if(isActive)
		{
			if(myRend.material.color != activeCol)
				myRend.material.color = activeCol;
		}
		else
		{
			if(myRend.material.color != inactiveCol)
				myRend.material.color = inactiveCol;
			colidedPlayer = null;
		}
	}
}
